package pms.dao.impl;


import pms.dao.CustomerDao;
import pms.dao.EmpDao;
import pms.dao.MedicineDao;
import pms.dao.SaleDao;
import pms.entity.Customer;
import pms.entity.Emp;
import pms.entity.Medicine;
import pms.entity.Sale;
import pms.until.JDBCTools;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SaleDaoImpl implements SaleDao {
    private JDBCTools tools=new JDBCTools();
    @Override
    public boolean addSaleForLeave(Sale sale) {
        int i= tools.update("insert into t_sale (sale_id,sale_emp,sale_customer,sale_medicine,sale_date,sale_number,sale_amount) values(?,?,?,?,?,?,?)",
                sale.getSale_id(),sale.getSale_emp().getEmp_id(),sale.getSale_customer().getCustomer_id(),sale.getSale_medicine().getMedicine_id(),sale.getSale_date(),sale.getSale_number(),sale.getSale_amount());
        return i>0;
    }

    @Override
    public List<Sale> querySaleByEmpidForLeave(int empid) {
        List<Sale> list=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_sale where sale_emp=? ",empid);
        try {
            while(rs.next()){
                Sale sale=new Sale();

                sale.setSale_date(rs.getDate("sale.date").toLocalDate());
                sale.setSale_number(rs.getInt("sale_number"));
                sale.setSale_amount(rs.getDouble("sale_amount"));
                EmpDao dao=new EmpDaoImpl();
                Emp emp=   dao.queryEmpById(rs.getInt("sale_emp"));
                sale.setSale_emp(emp);
                CustomerDao dao1=new CustomerDaoImpl();
                Customer customer=dao1.queryCustomerById(rs.getInt("sale_customer"));
                sale.setSale_customer(customer);
                MedicineDao dao2=new MedicineDaoImpl();
                Medicine medicine=dao2.qurryMedicineByID(rs.getInt("sale_medicine"));
                sale.setSale_medicine(medicine);
                list.add(sale);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return list;

    }

    @Override
    public boolean querysaleBySaleForLeave(Sale sale) {
        boolean flag=false;
        String sql="select * from t_sale where sale_emp=? and sale_id=? and sale_customer=? and sale_medicine=? and sale_date=? and sale_number=? and sale_amount=?";
        ResultSet rs= tools.query(sql,sale.getSale_emp().getEmp_id(),sale.getSale_id(),sale.getSale_customer().getCustomer_id(),sale.getSale_medicine().getMedicine_id(),sale.getSale_date(),sale.getSale_number(),sale.getSale_amount());
        try {
            if(rs.next()){
                flag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return flag;
    }

    @Override
    public boolean deleteSaleForLeave(int medicineid, int customerid, int empid) {
        int i= tools.update("delete from t_sale where sale_medicine=? and sale_customer=? and sale_emp=?",medicineid,customerid,empid
        );
        return i>0;
    }

    @Override
    public boolean querysaleByMCEForLeave(int medicineid, int customerid, int empid) {
        boolean flag=false;
        String sql="select * from t_sale where sale_medicine=? and sale_customer=? and sale_emp=? ";
        ResultSet rs= tools.query(sql,medicineid,customerid,empid);
        try {
            if(rs.next()){
                flag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return flag;
    }
}
